var express = require('express');
var router = express.Router();
// 引入
var classifyService = require('../service/classifyService');
/**
 * @swagger
 * /classify:
 *    post:
 *      tags:
 *      - classify
 *      summary: 增加分类的类型
 *      requestBody:
 *        required: true
 *        content:
 *          application/x-www-form-urlencoded:
 *            schema:
 *              type: object
 *              properties:
 *                sort:          
 *                  type: string
 *      responses:
 *        200:
 *          description: 操作成功
 *        401:
 *          description: 权限不足
 * */

// 增加
router.post('/', async function (req, res) {//'/add'改为'/'
  let { sort, state } = req.body;

  let data = await classifyService.insert({ sort, state });
  res.send({ data });
});
/**
 * @swagger
 * /classify:
 *    post:
 *      tags:
 *      - classify
 *      summary: 验证分类是否重复
 *      requestBody:
 *        required: true
 *        content:
 *          application/x-www-form-urlencoded:
 *            schema:
 *              type: object
 *              properties:
 *                sort:          
 *                  type: string
 *      responses:
 *        200:
 *          description: 操作成功
 *        401:
 *          description: 权限不足
 * */
// 验证(查找)
router.post('/classifyExist', async function (req, res) {
  let { sort } = req.body;
  let data = await classifyService.classifynameExist(sort);
  console.log("data:", data);
  if (data.length > 0) {
    res.send({
      status: 0
    });
  } else {
    res.send({
      status: 1
    });
  }
});

/**
 * @swagger
 * /classify/{id}:
 *    delete:
 *      tags:
 *      - classify
 *      summary: 删除类型
 *      parameters:
 *      - name: id
 *        in: path
 *        description: 类型id
 *        required: true      
 *      responses:
 *        200:
 *          description: 操作成功
 *        401:
 *          description: 权限不足
 * */


// 删除
router.delete('/:id', async function (req, res) {
  let { id } = req.params;
  let data = await classifyService.remove(id);
  console.log('data:', data);
  res.send({ data });

});

/**
 * @swagger
 * /classify:
 *    get:
 *      summary: 根据条件类型
 *      tags:
 *       - classify
 *      parameters:
 *      - name: type
 *        in: query
 *        description: 搜索类型
 *        required: false
 *      - name: value
 *        in: query
 *        description: 搜索值
 *        required: false
 *      - name: current
 *        in: query
 *        description: 当前页
 *        required: false
 *      - name: pageSize
 *        in: query
 *        description: 每页显示数
 *        required: false
 *      responses:
 *        200:
 *          description: 操作成功
 *        401:
 *          description: 权限不足
 * */

// 搜索
router.get('/', async function (req, res) {
  console.log("req.query", req.query);
  let { type, value, current, pageSize } = req.query;
  let searchObj = {};
  searchObj[type] = { $regex: value };
  searchObj.state = 1;
  let data = await classifyService.find(searchObj,
    { current: current || 1, pageSize: pageSize || 5 }//current页面显示的搜索出来的结果的第几页，若不从search.js文件中上传current : 1，页面显示的搜索出来的结果页码是当前页面开始搜索的第几页搜索出来的结果的第几页，就不会从第一页开始显示搜索出来的结果，如果结果数据比较少，页面就不会显示，因为大于第一页都没有内容
  );
  res.send({ data });
});



/**
 * @swagger
 * /classify/all:
 *    get:
 *      tags:
 *      - classify
 *      summary: 获取所有的类型
 *      responses:
 *        200:
 *          description: 操作成功
 *        401:
 *          description: 权限不足
 * */
// 搜索所有
router.get('/all', async function (req, res) {
  let data = await classifyService.findAll({ state: 1 });

  // let rows = data.filter((item, index) => item.state == 1);
  // data = rows
  // console.log(data);
  res.send({
    data
  });
});

/**
 * @swagger
 * /classify/{id}:
 *    get:
 *      summary: 根据id查询(修改)
 *      tags:
 *       - classify
 *      parameters:
 *      - name: id
 *        in: path
 *        description: 分类id
 *        required: true
 *      responses:
 *        200:
 *          description: 操作成功
 *        401:
 *          description: 权限不足
 * */

// 修改搜索
router.get('/:id', async function (req, res) {
  console.log("req.query", req.query);
  let { id } = req.params;
  let data = await classifyService.findById(id);
  console.log("修改搜索参数2：", id);
  res.send({ data });
});


/**
 * @swagger
 * /classify/{id}:
 *    put:
 *      tags:
 *      - classify
 *      summary: 修改类型
 *      parameters:
 *      - name: id
 *        in: path
 *        description: 类型的id
 *        required: true
 *      requestBody:
 *        required: true
 *        content:
 *          application/x-www-form-urlencoded:
 *            schema:
 *              type: object
 *              properties:
 *                sort:          
 *                  type: string
 *      responses:
 *        200:
 *          description: 操作成功
 *        401:
 *          description: 权限不足
 * */
// 修改
router.put('/:id', async function (req, res) {
  console.log("req.body:", req.body);
  let { id } = req.params;
  let { sort, state } = req.body;
  let data = await classifyService.update(id, { sort, state });
  res.send({ data });

});








module.exports = router;
